

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Talk extends Thread{
    private Socket socket;
    private String user;


    public Talk(Socket socket,String user) {
        this.socket = socket;
        this.user=user;

    }

    @Override
    public synchronized void start() {
        super.start();
    }

    @Override
    public void run() {
        super.run();
        try(
                Scanner scanner=new Scanner(System.in);
                OutputStream ots=socket.getOutputStream();
                OutputStreamWriter ost=new OutputStreamWriter(ots, StandardCharsets.UTF_8);
                BufferedWriter bfw=new BufferedWriter(ost);
                PrintWriter prw=new PrintWriter(bfw,true);
        ) {



            String line;
            while (!"exit".equals(line= scanner.nextLine())){
                prw.println(user+" say:"+line);
                Thread.sleep(500);
            }

        } catch (IOException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
